*************
* SCRIPT FOR: How local context affects populist radical right support: A cross-national investigation into mediated and moderated relationships
* By Kai Arzheimer, Carl Berning, Sarah de Lange, Jerome Dutozia, Jocelyn Evans, Myles Gould, Eelco Harteveld, Nick Hood, Gilles Ivaldi, Paul Norman, Wouter van der Brug, and Tom van der Meer.
* Published in British Journal of Political Science
*
*
* Data available at:
* - UK: http://doi.org/10.5255/UKDA-SN-8474-1
* - DE: https://doi.org/10.7910/DVN/X8K8L3
* - NL: https://doi.org/10.17026/dans-znu-4wt8
* - FR: 
* - Harmonized file: https://doi.org/10.7910/DVN/YETU0O
*
* Notes:
*   1. This file contains the multilevel regressions to estimate interactions. For the Structural Equation Models, see the other file. These are MPLUS files that include the source correlations.
*   2. For privacy reasons, the publicly shared harmonized file on Dataverse does not include values for geoidentifiers or contextual variables. These are directly available from the authors if requested, and if very strict undertakings were given not to share the data.


* Part 1: Merge data and harmonize
* --------------------------------

* 1.2. Merge data

use "German_harmon.dta", clear
egen ID_LEVEL2b = group(ID_LEVEL2), label
drop ID_LEVEL2
gen ID_LEVEL2 = 300000 + ID_LEVEL2b
gen country=3

append using "FR_harmon.dta", gen(data_FR)
replace country=2 if data_FR==1
egen ID_LEVEL2c = group(ID_LEVEL2) if country==2, label
replace ID_LEVEL2 = 200000 + ID_LEVEL2c if country==2

append using "UK_harmon.dta", gen(data_UK)
replace country=1 if data_UK==1
egen ID_LEVEL2d = group(ID_LEVEL2) if country==1, label
replace ID_LEVEL2 = 100000 + ID_LEVEL2d if country==1

append using "NL_harmon.dta", gen(data_NL)
replace country=4 if data_NL==1
egen ID_LEVEL2e = group(ID_LEVEL2) if country==4, label
replace ID_LEVEL2 = 500000 + ID_LEVEL2e if country==4

egen threat_nl=rowmean(thrt_cul1 thrt_cul2 thrt_eco1 thrt_eco2)
replace threat=threat_nl if country==4

lab define country 1 "UK" 2 "FR" 3 "DE" 4 "NL"
lab values country country

* 1.2. Harmonize

replace age0to15_lvl2 = age0to15/100 if country==1
replace age16to24_lvl2 = age16to24/100 if country==1
replace age25to44_lvl2 = age25to44/100 if country==1
replace age45to64_lvl2 = age45to64/100 if country==1
replace age65_lvl2 = age65/100 if country==1

replace age0to15_lvl2 = age017_LEVEL2 if country==2
replace age16to24_lvl2 = age1824_LEVEL2 if country==2
replace age25to44_lvl2 = age2539_LEVEL2 if country==2

replace age0to15_lvl2 = lvl2_age0to14 if country==3
replace age16to24_lvl2 = lvl2_age15to24 if country==3
replace age25to44_lvl2 = lvl2_age25to49 if country==3
replace age45to64_lvl2 = lvl2_age50to64 if country==3
replace age65_lvl2 = lvl2_age65 if country==3

replace migration_lvl2 = (100-uk)/100 - eu/100 if country==1
replace migration_lvl2 = IMMIG_LEVEL2 if country==2
replace migration_lvl2 = lvl2_non_eu if country==3
replace unemployment_lvl2 = LEVEL2_PCT_UNEMPLOYED/100 if country==1
replace unemployment_lvl2 = UNEMP_LEVEL2/100 if country==2
replace unemployment_lvl2 = lvl2_unemployment if country==3

replace edu = 5-edu if country==1 | country==2 
replace edu = edu4 if country==4
lab define edu 1 "Lowest" 4 "Highest", replace
lab values edu edu

foreach var of varlist migration_lvl2 unemployment_lvl2 age16to24_lvl2 age25to44_lvl2 {
	egen mean_`var' = mean(`var'), by(country)
	egen sd_`var' = sd(`var'), by(country)
	gen std_`var' = (`var' - mean_`var') / sd_`var'
	}


* Part 2: Predicting perceived local decline
* --------------------------------------------

* 2.1. By education
local vars migration_lvl2 unemployment_lvl2 age16to24_lvl2 age25to44_lvl2
foreach var of varlist `vars' {
	local labvar : variable label `var'
	display `labvar'
	logit worse c.`var'##i.edu##i.country c.age##c.age i.edu male `vars' density, cluster(ID_LEVEL2)
	estimates store edu_`var'
	margins, dydx(`var') over(country edu) 
	marginsplot, scheme(plotplainblind) plotopts(connect(none)) recastci(rarea) ciopts(color(maroon%50)) by(country) byopts(cols(1) title("Test")) ysize(9) yline(0) legend(off) xtitle("") ytitle("") name(`var', replace) 
	}
local vars migration_lvl2 unemployment_lvl2 age16to24_lvl2 age25to44_lvl2
graph combine `vars', scheme(plotplainblind) cols(4) xsize(5) ycommon
graph export "Figure 5.png", replace width(5000)

* 2.2. By neigbourhood embeddedness
local vars migration_lvl2 unemployment_lvl2 age16to24_lvl2 age25to44_lvl2
foreach var of varlist `vars' {
	local lab: variable label `var' 
	logit worse c.`var'##c.id_nhood##i.country c.age##c.age i.edu male `vars' density, cluster(ID_LEVEL2)
	estimates store id_`var'
	margins, dydx(`var') over(country id_nhood) 
	marginsplot, scheme(plotplainblind) plotopts(connect(none)) recastci(rarea) ciopts(color(maroon%50)) by(country) byopts(cols(1) title("Test")) ysize(9) yline(0) legend(off) xlab(1 `" "1"  "Not" "attached" "' 7 `" "7"  "Strongly" "attached" "') xtitle("") ytitle("") name(`var', replace)
	}
local vars migration_lvl2 unemployment_lvl2 age16to24_lvl2 age25to44_lvl2
graph combine `vars', scheme(plotplainblind) cols(4) xsize(5) ycommon
graph export "Figure 6.png", replace width(5000)

* Part 3: Predicting PRR support
* ------------------------------

* 3.1. By education
local vars migration_lvl2 unemployment_lvl2 age16to24_lvl2 age25to44_lvl2
foreach var of varlist `vars' {
	local lab: variable label `var' 
	logit prr c.`var'##i.edu##i.country c.age##c.age i.edu male `vars' density, cluster(ID_LEVEL2)
	margins, dydx(`var') over(country edu) 
	marginsplot, scheme(plotplainblind) plotopts(connect(none)) recastci(rarea) ciopts(color(maroon%50)) by(country) byopts(cols(1) title("`lab'")) ysize(9) yline(0) legend(off) xtitle("") ytitle("") name(`var', replace)
	}
local vars migration_lvl2 unemployment_lvl2 age16to24_lvl2 age25to44_lvl2
graph combine `vars', scheme(plotplainblind) cols(4) xsize(5) ycommon
graph export "Figure 7.png", replace width(5000)

* 3.2 By neigbourhood embeddedness
local vars migration_lvl2 unemployment_lvl2 age16to24_lvl2 age25to44_lvl2
foreach var of varlist `vars' {
	local lab: variable label `var' 
	logit prr c.`var'##c.id_nhood##i.country c.age##c.age i.edu male `vars' density, cluster(ID_LEVEL2)
	*margins, at(`var'=(-2(0.5)2)) over(edu country) predict(outcome(1))
	margins, dydx(`var') over(country id_nhood) 
	marginsplot, scheme(plotplainblind) plotopts(connect(none)) recastci(rarea) ciopts(color(maroon%50)) by(country) byopts(cols(1) title("`lab'")) ysize(9) yline(0) legend(off) xlab(1 `" "1"  "Not" "attached" "' 7 `" "7"  "Strongly" "attached" "') xtitle("") ytitle("") name(`var', replace)
	}
local vars migration_lvl2 unemployment_lvl2 age16to24_lvl2 age25to44_lvl2
graph combine `vars', scheme(plotplainblind) cols(4) xsize(5) ycommon
graph export "Figure 8.png", replace width(8000)


